
################################################################################
# Judicial Ideology as Text -- Figure A2: Static Wordshoal Data Summary by Justice & Term
# Truscott & Romano
# Updated February 2025
################################################################################

################################################################################
# Necessary Packages & Dependencies
################################################################################

source('R/truscott_romano_source.R')

################################################################################
# Retrieve Wordshoal & Decisions Data
################################################################################

wordshoal_static_summaries <- data.frame()

for (weight in 1:length(wordshoals_static)){

  wordshoal_static_summaries <- bind_rows(wordshoal_static_summaries, wordshoals_static[[1]]$psi_doc)

}

wordshoal_static_summaries <- wordshoal_static_summaries %>%
  distinct(opinion_id) %>%
  left_join(decisions, by = 'opinion_id', relationship = 'many-to-many') %>%
  distinct(opinion_id, .keep_all = T) %>%
  dplyr::select(docket, opinion_id, term, author_last, issue_area, minVotes, vote) %>%
  rename(decision_type = vote)

{

  Figure_A2 <- wordshoal_static_summaries %>%
    mutate(decision_type = case_when(
      .default = '(Special) Concurrence\n(In Judgement)',
      decision_type == 'Majority' ~ 'Majority',
      decision_type %in% c('Dissent', 'Jurisdictional Dissent') ~ '(Jurisdictional)\nDissent'),
      decision_type = factor(decision_type, levels = c('Majority', '(Special) Concurrence\n(In Judgement)', '(Jurisdictional)\nDissent'))) %>%
    group_by(term, decision_type, author_last) %>%
    summarise(count = n()) %>%
    ungroup() %>%
    mutate(author_last = ifelse(author_last == 'Connor', "O'Connor", author_last)) %>%
    ggplot(aes(x = as.numeric(term), y = count)) +
    geom_col(aes(fill = decision_type), colour = 'black', position = position_stack(reverse = T)) +
    facet_wrap(~author_last) +
    scale_y_continuous(breaks = seq(5, 25, 5)) +
    geom_hline(yintercept = 0, colour = 'black') +
    scale_fill_manual(values = c('deepskyblue3', 'cornsilk', 'coral3')) +
    labs(x = '\nTerm',
         y = 'Count\n') +
    theme_minimal() +
    theme(panel.border = element_rect(size = 1, colour = 'black', fill = NA),
          axis.text = element_text(size = 14, colour = 'black'),
          axis.title = element_text(size = 16, colour = 'black'),
          strip.background = element_rect(size = 1, colour = 'black', fill = 'grey'),
          strip.text = element_text(size = 14, colour = 'black'),
          legend.position = 'bottom',
          legend.title = element_blank(),
          legend.background = element_rect(size = 1, colour = 'black', fill = NA),
          legend.text = element_text(size = 14, colour = 'black', hjust = 0.5))


  ggsave(Figure_A2,
         file = 'figures/Figure_A2.png',
         width = 10,
         height = 10,
         units = 'in',
         bg = 'white')


  ggsave('figures_tiff/Figure_A2.tiff',
         plot = Figure_A2,
         device = 'tiff',
         dpi = 600,
         width = 10,
         height = 10,
         units = 'in',
         bg = 'white')

} # Opinions by Term & Authoring Justice (Appendix Figure 2)

rm(wordshoal_static_summaries)
